package homework.level01model03.homework04;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentDelete {

    public static void delete(ArrayList<Student> arrayList){

        Scanner sc = new Scanner(System.in);
        System.out.println("删除人的姓名:");
        String name = sc.nextLine();


        int size = arrayList.size();
        if (size==0){
            System.out.println("删除失败");
        }else{
            //找到删除对象的index  默认-1

            int index=-1;
            for (Student student : arrayList) {
                if (name.equals(student.getName())) {
                    index = arrayList.indexOf(student);
                    break;
                }
            }

            //如果仍是默认值 说明没找到
            if (index==-1){
                System.out.println("查无此人，删除失败");
            }else{  //index变了说明找到了 根据索引remove
                arrayList.remove(index);
                System.out.println("删除成功");
            }

        }

        //遍历集合全部元素
        System.out.println("ok??");
        for (Student student : arrayList) {
            System.out.println(student);
        }




    }
}
